
 function FontLoader(pdDoc){
	  this.pdDoc = pdDoc;
	  this.pdPage = null;
	  this.fontFamilies = [];
	  this.callback = null;
	  this.caller = null;
	  this.pageIndex = 0;
	  this.loadFonts = [];
	  
	  /* updatae 2013.12.18 */
	  this.standFont = ""; //common font library path directory
	  this.webFont = ""; //webfont.js path
	  
	 
	  this.LoadFamilies = function(){
		
		  var fontInfoArr = this.pdDoc.fontInfoList;
		  //if(fontInfoArr.length == 0 || fontInfoArr == null)return;
		  
		  for(var i = 0; i < fontInfoArr.length; i++){
			  if(fontInfoArr[i] == null || fontInfoArr[i].fontType == 3)continue;
			  this.GetFontFamily(fontInfoArr[i]);
			   
		  }
		  if(this.loadFonts.length > 0)this.InitFont();
		  else{
			 var loading = document.getElementById("loading");
			  if(loading != undefined)loading.style.display = "none";
			  if(this.caller == null)this.callback.call();
			  else this.callback.call(this.caller);
		  }
	  }
	  
	  this.LoadFamiliesByPage = function(){
		 
		  this.pdPage = this.pdDoc.GetPageByIndex(this.pageIndex);
		  if(this.pdPage == undefined || this.pdPage == null)return;
		  var usedFont = this.pdPage.pdPageObjects.m_usedFont;
		  this.loadFonts = [];
		  for(var i = 0; i < usedFont.length; i++){
			  var fontInfo = usedFont[i];
			  if(fontInfo == null || fontInfo.fontType == 3)continue;
			  this.GetFontFamily(fontInfo);
		  }
		  if(this.loadFonts.length > 0)this.InitFont();
		  else{
			 if(document.getElementById("loading")!=undefined) document.getElementById("loading").style.display = "none";
			 if(this.caller == null)this.callback.call();
			  else this.callback.call(this.caller);
		  }
	  };
	  
	  this.GetFontFamily = function(fontInfo){
		  var family = fontInfo.fontFace;
		  var fontName = family;
		  var temp = family.split("_");
          if(fontInfo.isStandard == 1 || temp[1] == "STD"){ //is standfont
        	fontName = baseFonts[temp[2]];
          }else if(temp[1] != "CJK" && temp[1] != "EMB" && isNaN(temp[1])){ //is commnfont
        	fontName = temp[1];
         }
		  
		   if(this.fontFamilies.in_array(family))return;
		  var fontPath = (temp[1] == "CJK" || temp[1] == "EMB" || !isNaN(temp[1]))?this.pdDoc.m_fontPath:this.standFont;
		  this.fontFamilies.push(family);
	  	  this.loadFonts.push(family);
		  this.CreateFontFace(family,fontName,fontPath);
	  };
	  
	  this.CreateFontFace = function(family,FXname,fontPath){
    	var headElement=document.getElementsByTagName("head")[0]; 
		var styleElements=headElement.getElementsByTagName("style"); 
		fontPath = fontPath.charAt(0) == "/"?fontPath.substr(1,fontPath.length):fontPath;
		if(fontPath.charAt(fontPath.length-1)!="/")fontPath = fontPath+"/";
		var cssCode = "@font-face{" +
	         "font-family:'" + family + "';" +
	         "src:url('" + fontPath+FXname + ".woff') format('woff');" + 
	     	"}";
		
    	 if (!document.styleSheets[family]) { 
    		 var ss = null;
    		 if(document.createStyleSheet){ 
					ss = document.createStyleSheet(); 
					ss.owningElement.id = family;
				}else{ 
					var tempStyleElement=document.createElement('style'); 
					tempStyleElement.setAttribute("type","text/css"); 
					tempStyleElement.setAttribute("id",family);
					headElement.appendChild(tempStyleElement); 
					
				} 
	 		 
	  		 var styleElement=styleElements[family];
	  		 if(styleElement.styleSheet){
				styleElement.styleSheet.cssText+=cssCode; 
			 }else if(document.getBoxObjectFor){ 
				styleElement.innerHTML+=cssCode;
			 }else{ 
				styleElement.appendChild(document.createTextNode(cssCode)) 
			 } 
	         
       }

    }
	
  }
  
FontLoader.prototype.InitFont = function(){
	
	WebFontConfig = {
	  		custom: { families: this.loadFonts,
			callback:this.callback,
			caller:this.caller,
			fontLoader:this
		},
    	loading: function() {
	 	},
		fontloading: function(fontFamily, fontDescription) {
		},
		fontactive: function(fontFamily, fontDescription) {
		},
		fontinactive: function(fontFamily, fontDescription) {
		  FontLoader.prototype.UpdateFontInfo.call(this.custom.fontLoader,fontFamily);
		},
		active: function() {
		    if(document.getElementById("loading")!=undefined)document.getElementById("loading").style.display = "none";
		    if(this.custom.caller == null)this.custom.callback(this.custom.pdDoc);
		    else{
		    	this.custom.callback.call(this.custom.caller);
		    }
		},
		inactive: function() { 
			if(document.getElementById("loading")!=undefined)document.getElementById("loading").style.display = "none";
		    if(this.custom.caller == null)this.custom.callback(this.custom.pdDoc);
		    else this.custom.callback.call(this.custom.caller);
		}
    	
	 };
	
	var wf = document.createElement('script');
    wf.src = this.webFont;
    wf.type = 'text/javascript';
    wf.async = 'true';
    var s = document.getElementsByTagName('script')[0];
    s.parentNode.insertBefore(wf, s);
}  
FontLoader.prototype.UpdateFontInfo = function(fontFamily){
	var fontInfo = this.pdDoc.GetFontInfoByFontFace(fontFamily);
	fontInfo.m_bValide = false;
}
Array.prototype.in_array = function(e)
{
for(i=0;i<this.length && this[i]!=e;i++);
return !(i==this.length);
}


